<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <h2 class="text-center  margin-bottom">{{title}}</h2>
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="时间筛选">
                            <a-range-picker
                                    :placeholder="['开始时间', '结束时间']"
                                    v-model="rangeTime"
                                    format="YYYY-MM-DD"
                                    @change="handleDateChange"
                            />
                        </a-form-item>
                    </a-col>

<!--                    <a-col :md="6" :sm="24">-->
<!--                        <a-form-item label="产生单位">-->
<!--                            <select-customer-list @change="changeCustomer"></select-customer-list>-->
<!--                        </a-form-item>-->
<!--                    </a-col>-->

                    <!--                    <a-col :md="6" :sm="24" v-has="'query:company:select:auth'">-->
                    <!--                        <a-form-item label="处置单位">-->
                    <!--                            <select-company-list @change="changeCompany"></select-company-list>-->
                    <!--                        </a-form-item>-->
                    <!--                    </a-col>-->

                    <!--                    <a-col :md="8" :sm="24">-->
                    <!--                        <a-form-item label="区域查询">-->
                    <!--                            <select-area @change="changeArea"></select-area>-->
                    <!--                        </a-form-item>-->
                    <!--                    </a-col>-->

                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <div class="table-operator" style="display: flex;justify-content: space-between;align-items: center;">
            <div>
                <a-button type="primary" @click="searchQuery" icon="search">查询数据列表</a-button>
                <a-button type="primary" @click="resetForm">重置查询</a-button>
            </div>
            <div>
                <a-button type="primary" icon="printer" v-print="'#printTableList'">打印</a-button>
            </div>
        </div>

        <!-- table区域-begin -->
        <div>
            <a-table
                    ref="table"
                    size="middle"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="dataSources"
                    :pagination="ipagination"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    @change="handleTableChange"
            >
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="link" slot-scope="text,record">
                    <a href="#" style="color: #3fe4f1;" @click="handleShowDetail(record)">详情</a>
                </template>
                <template slot="imgSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
                    <img v-else :src="text" height="25px" alt style="width: auto;height: 50px;"/>
                </template>
            </a-table>
        </div>

        <div v-show="false">
            <div id="printTableList">
                <h2 style="text-align: center;font-weight: bold;">
                    {{title}}
                </h2>
                <h3 v-if="rangeTime.length">时间 ： {{rangeTime[0]}} 至 {{rangeTime[1]}}</h3>
                <a-table
                        size="small"
                        bordered
                        rowKey="id"
                        :columns="columns"
                        :dataSource="dataSource"
                        :pagination="false"
                >
                    <template slot="htmlSlot" slot-scope="text">
                        <div style="color: black !important;" v-html="text"></div>
                    </template>
                </a-table>
            </div>
        </div>
    </a-card>
</template>

<script>
    /**
     * create by Jane
     * 二维码回溯
     */
    import '@/assets/less/TableExpand.less'
    import { mixinDevice } from '@/utils/mixin'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import SelectArea from '@/components/Jane/SelectArea'
    import SelectCompanyList from '@/components/Jane/SelectCompanyList'
    import SelectCustomerList from '@/components/Jane/SelectCustomerList'

    export default {
        mixins: [JeecgListMixin, mixinDevice],
        components: {
            SelectArea,
            SelectCompanyList,
            SelectCustomerList
        },
        data() {
            return {
                title: '产品发放 - 数据统计',
                // 表头
                columns: [],
                url: {
                    list: '/hc/hcCommodityReleaseRecord/getCommodityReleaseRecord'
                },
                rangeTime: [],
                queryParam: {
                    creatTimeBegin: null,
                    creatTimeEnd: null,
                    customerName: ''
                }
            }
        },
        mounted() {
        },
        computed:{
            dataSources(){
                let list = [];
                this.dataSource.forEach((item,index) => {
                    let info = list.find((v) => v.createName == item.createName);
                    if(info){
                        info['rowSpan']++;
                    }else{
                        list.push({
                            createName : item.createName,
                            rowSpan : 1,
                            index
                        })
                    }
                })
                let data = [...this.dataSource];
                data.forEach((item,index) => {
                    item['rowSpan'] = 0;
                    let info = list.find((v) => v.index == index);
                    if(info && info['rowSpan']){
                        item['rowSpan'] = info['rowSpan'];
                    }
                })
                return data;
            }
        },
        methods: {
            async initDictConfig() {

                // 获取到用户登陆的信息
                let userInfo = this.$store.getters.userInfo
                if (userInfo.companysId) {
                    this.queryParam.companysId = userInfo.companysId
                }

                this.queryParam.creatTimeBegin = new Date().MonthFirstDay()
                this.queryParam.creatTimeEnd = new Date().MonthLastDay()
                this.rangeTime = [this.queryParam.creatTimeBegin, this.queryParam.creatTimeEnd]

                this.setColumns()
            },
            // 重置筛选表单
            resetForm(){
                this.queryParam.companysId = '';
                this.queryParam.customerNumber = '';

                this.initDictConfig();
                this.searchQuery();
            },

            handleDateChange(value, dateString) {
                this.rangeTime = dateString
                if (dateString[0]) {
                    this.queryParam.creatTimeBegin = dateString[0]
                }
                if (dateString[1]) {
                    this.queryParam.creatTimeEnd = dateString[1]
                }
                this.searchQuery();
            },
            async changeArea({ area, city }) {
                if (city) {
                    this.queryParam.cityCode = city
                }
                if (area) {
                    this.queryParam.areaCode = area
                }
            },
            async changeCustomer(value, option) {
                this.queryParam.customerNumber = value
            },
            async changeCompany(value, option) {
                this.queryParam.companysId = value
            },
            setColumns() {
                this.columns = [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '操作人',
                        align: 'center',
                        dataIndex: 'createName',
                        customRender: (value, row, index) => {
                            const obj = {
                                children: value,
                                attrs: {},
                            };
                            if(row.rowSpan){
                                obj.attrs.rowSpan = row.rowSpan;
                            }else{
                                obj.attrs.rowSpan = 0;
                            }
                            return obj;
                        },
                    },
                    {
                        title: `产品信息`,
                        dataIndex: 'goodsName',
                        align: 'center'
                    },
                    {
                        title: `发放数量`,
                        dataIndex: 'num',
                        align: 'center'
                    },
                    // {
                    //     title: `发放产品信息信息`,
                    //     align: 'center',
                    //     children: [
                    //         {
                    //             title: `产品信息`,
                    //             dataIndex: 'goodsName',
                    //             align: 'center'
                    //         },
                    //         {
                    //             title: `发放数量`,
                    //             dataIndex: 'num',
                    //             align: 'center'
                    //         },
                    //         // {
                    //         //     title: `产品单价`,
                    //         //     dataIndex: 'price',
                    //         //     align: 'center',
                    //         //     scopedSlots: { customRender: 'htmlSlot' },
                    //         //     customRender: function(value, row, index) {
                    //         //         const obj = {
                    //         //             children: <span style = 'font-weight:600;' >￥ {value} < /span>,
                    //         //             attrs: {}
                    //         //         }
                    //         //         return obj
                    //         //     }
                    //         // }
                    //     ]
                    //
                    // }
                ]
            },
            // 废物类型选择改变
            changeWasteType(info, wasteList) {
                this.setColumns()
            },
            searchQuery: function() {
                this.loadData(1)
            }

        }
    }
</script>
<style scoped>
    @import '~@assets/less/common.less';
</style>
